package com.company.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.aop.annotation.LogAnnotation;
import com.company.project.common.utils.Constant;
import com.company.project.entity.BusiPatrolEntity;
import com.company.project.entity.SysUser;
import com.company.project.service.HttpSessionService;
import com.company.project.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import com.company.project.common.utils.DataResult;

import com.company.project.entity.BusiUrgentEntity;
import com.company.project.service.BusiUrgentService;

import javax.annotation.Resource;


/**
 * 紧急任务
 *
 * @author zhanggm
 * @email *****@mail.com
 * @date 2021-06-15 09:54:41
 */
@Controller
@RequestMapping("/")
public class BusiUrgentController {
    @Autowired
    private BusiUrgentService busiUrgentService;
    @Resource
    private UserService userService;
    @Resource
    private HttpSessionService httpSessionService;


    /**
    * 跳转到页面
    */
    @GetMapping("/index/busiUrgent")
    public String busiUrgent() {
        return "busiurgent/list";
        }

    @ApiOperation(value = "新增")
    @PostMapping("busiUrgent/add")
    @RequiresPermissions("busiUrgent:add")
    @LogAnnotation(title = "紧急任务", action = "新增")
    @ResponseBody
    public DataResult add(@RequestBody BusiUrgentEntity busiUrgent){
        busiUrgentService.wholeAdd(busiUrgent);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("busiUrgent/delete")
    @RequiresPermissions("busiUrgent:delete")
    @LogAnnotation(title = "紧急任务", action = "删除")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        busiUrgentService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("busiUrgent/update")
    @RequiresPermissions("busiUrgent:update")
    @LogAnnotation(title = "紧急任务", action = "更新")
    @ResponseBody
    public DataResult update(@RequestBody BusiUrgentEntity busiUrgent){
        busiUrgentService.updateById(busiUrgent);
        return DataResult.success();
    }
    @ApiOperation(value = "紧急任务处理")
    @PutMapping("busiUrgent/deal")
    @RequiresPermissions("busiUrgent:update")
    @LogAnnotation(title = "紧急任务", action = "紧急任务处理")
    @ResponseBody
    public DataResult deal(@RequestBody BusiUrgentEntity busiUrgent){
        busiUrgentService.wholeDeal(busiUrgent);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("busiUrgent/listByPage")
    @RequiresPermissions("busiUrgent:list")
    @LogAnnotation(title = "紧急任务", action = "查询分页数据")
    @ResponseBody
    public DataResult findListByPage(@RequestBody BusiUrgentEntity busiUrgent){
        Page page = new Page(busiUrgent.getPage(), busiUrgent.getLimit());
        LambdaQueryWrapper<BusiUrgentEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        // 如果是APP用户，则只能查询到自己的数据.如果是工作人员或系统管理员，则可以查看所有数据
        String userId= httpSessionService.getCurrentUserId();
        SysUser sysUser = userService.getById(userId);
        // 查看数据来源是否为APP
        if (Constant.APP.equals(sysUser.getCreateWhere())) {
            queryWrapper.eq(BusiUrgentEntity::getUserId, sysUser.getId());
        }
        // 起始时间
        String startTime = busiUrgent.getStartTime();
        String endTime = busiUrgent.getEndTime();
        if (StringUtils.isNotBlank(startTime)) {
            queryWrapper.gt(BusiUrgentEntity::getCreateTime, startTime);
        }
        if (StringUtils.isNotBlank(endTime)) {
            queryWrapper.lt(BusiUrgentEntity::getCreateTime, endTime);
        }
        queryWrapper.orderByDesc(BusiUrgentEntity::getCreateTime);
        IPage<BusiUrgentEntity> iPage = busiUrgentService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

}
